package com.test.map;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.test.map.HTTP.OKHttp;
import com.test.map.Users.Order;
import com.test.map.sign.BaseActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import okhttp3.FormBody;

public class WaitDriver extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    private CheckBox[] checkBoxes = new CheckBox[2];
    private TextView place;
    private TextView cost;
    int type=0;
    public FormBody body;
    private OKHttp okHttp;
    private Order order;
    private String order_id;
    private ProgressDialog progressDialog;
    private String poiLat,poiLon,mLat,mLon;
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent=getIntent();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wait_driver);
        ArrayList<String> infoList = new ArrayList<String>();
        infoList = getIntent().getStringArrayListExtra("infoList");
        // textView=(TextView)findViewById(R.id.text_1) ;
        //  textView.setText("Hi,"+intent.getStringExtra("ex"));//显示你的名字出来
        checkBoxes[0] = (CheckBox) findViewById(R.id.guest1);
        checkBoxes[1] = (CheckBox) findViewById(R.id.driver1);
        checkBoxes[0].setOnCheckedChangeListener( this);
        checkBoxes[1].setOnCheckedChangeListener( this);
        Button button=(Button)findViewById(R.id.sure);
        place = (TextView) findViewById(R.id.place);
        cost = (TextView) findViewById(R.id.cost);

        cost.setText("费用："+infoList.get(0));
        place.setText("目的地："+infoList.get(1));

        final ArrayList<String> finalInfoList = infoList;
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long currentTime = System.currentTimeMillis();
                String.valueOf(System.currentTimeMillis());
                String timeNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);

                App app=(App) getApplication();
                app.getUid();

                body = new FormBody.Builder()
                        .add("sourceName",finalInfoList.get(6))
                        .add("sourceLatitude", finalInfoList.get(2))
                        .add("sourceLongitude", finalInfoList.get(3))
                        .add("destName",finalInfoList.get(1))
                        .add("destLatitude", finalInfoList.get(4))
                        .add("destLongitude", finalInfoList.get(5))
                        .add("startTime", String.valueOf(System.currentTimeMillis()))
                        .add("uid", app.getUid())

                        .build();
                okHttp= new OKHttp();
              //  Log.d("123", okHttp.s);
                initProgressDialog();

                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        try {
                            okHttp.post("http://47.105.179.182:5000/preorder",body);
                            Thread.sleep(2*1000);
                            String a=okHttp.s;
                            JSONObject Order=new JSONObject(a);

                            Order.getString("orderId");


                          //  progressDialog.cancel();



                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                }).start();
                waitordered();

            }
        });



    }
    private void initProgressDialog() {
        progressDialog = new ProgressDialog(WaitDriver.this);
        progressDialog.setIndeterminate(true);//循环滚动
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Waiting...");
        progressDialog.setCancelable(true);//false不能取消显示，true可以取消显示*/

        //order.parsing(okHttp.Requst("http://guolin.tech/api/china"));
        //PollingUtils.startPollingService(this, 1,PollingService.class, WaitDriver.ACTIVITY_SERVICE);
        progressDialog.show();

        //startLoopService();

    }
    public void waitordered() {


        new Thread(new Runnable(){
            @Override
            public void run() {
                try {
                    okHttp=new OKHttp();
                    body = new FormBody.Builder().build();
                    do{
                        okHttp.post(" http://47.105.179.182:5000/orderStatus/26",body);
                        Thread.sleep(2*1000);

                    }while (okHttp.s.equals(""));
                         String a=okHttp.s;
                        JSONObject Order=new JSONObject(a);
                        Order order_in=new Order().parsing(Order.getJSONObject("result"));

                    progressDialog.cancel();

                    Intent intent = new Intent();
                    ArrayList<String> info = new ArrayList<String>();
                    info.add(order_in.getId());
                    info.add(order_in.getCar_type());
                    info.add(order_in.getCar_id());
                    info.add(order_in.getPassenger());
                    info.add(order_in.getType());
                    info.add(order_in.getFinished());
                    info.add(order_in.getEnd_place());
                    info.add(order_in.getStart_place());
                    info.add(order_in.getCost());

                    intent.putStringArrayListExtra("order_finish",info );
                    intent.setClass(WaitDriver.this, Finish_order.class);
                    startActivity(intent);



                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }).start();





        //LoopService loopService=new LoopService();
        //loopService.startLoopService(this,progressDialog,body);

        // 启动轮询服务
        // 暂时不考虑加入网络情况的判断...
        /*if (!Handler.LoopService.isServiceRuning) {
            // 用户是登录态，启动轮询服务
            if (UserConfig.isPassLogined()) {
                // 判断当前长连接的状态，若长连接已连接，则不再开启轮询服务
                if (MinaLongConnectManager.session != null && MinaLongConnectManager.session.isConnected()) {
                    LoopService.quitLoopService(context);
                    return;
                }

            } else {
                LoopService.quitLoopService(context);
            }
        }*/
    }


    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b) {
            for (int i = 0; i < checkBoxes.length; i++) {
                //不等于当前选中的就变成false
                if (checkBoxes[i].getText().toString().equals(compoundButton.getText().toString())) {
                    checkBoxes[i].setChecked(true);
                    type=i;
                } else {
                    checkBoxes[i].setChecked(false);
                }
            }
        }
    }
}
